﻿using System;
using System.Collections.Generic;
using System.Text;
using Cemit.PolyProto;
using Google.Protobuf;

namespace Cemit.PolyServer
{
    class CreatePlayerHandle : IMessageHandle<Msg_Lobby_CreatePlayer>
    {
        public void Execute(ClientState clientState, IMessage message)
        {
            Msg_Lobby_CreatePlayer msg = message as Msg_Lobby_CreatePlayer;

            if (clientState.server.Players.Contains(msg.PlayerData.Id))
            {
                //返回一条空角色数据消息，表示创建角色失败
                clientState.Send(new Msg_Lobby_CreatePlayer()
                {
                    PlayerData = null
                });

                return;
            }

            //添加为在线的玩家
            Player player = new Player(clientState, msg.PlayerData.Id);
            clientState.server.Players.AddPlayer(player);
            this.Log("玩家已上线：" + player.ID);

            //告知所有玩家该玩家已上线
            //PolyServer.SendToAllPlayers(msg);

            //获取所有玩家当前的信息，并发送给该玩家用于创建角色
            Msg_Lobby_GetPlayerData msgGetPlayer = new Msg_Lobby_GetPlayerData
            {
                UsedToCreate = true,
                Requester = player.ID
            };
            //PolyServer.SendToAllPlayers(msgGetPlayer, player);
        }
    }
}
